package com.portalEsporteClubeBahia.app.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.ConstantsIf;

public class DAOFactory {

	private Connection conn = null;
	
	public DAOFactory() throws ApplicationException{
	
		try{
			if(conn == null || conn.isClosed()){
				Class.forName(ConstantsIf.DATABASE_DRIVER);
				conn = DriverManager.getConnection(ConstantsIf.DATABASE_URL, ConstantsIf.DATABASE_USER, ConstantsIf.DATABASE_PASSWORD);
			}
			
		} catch (Exception e){      
			throw new ApplicationException(e);    
		}  
	
	}
	
	public Connection getConnection(){
		return conn;
	}
	
	public void close(Connection conn, PreparedStatement ps, ResultSet rs) throws ApplicationException{
		try {
			if(rs != null){
				rs.close();	
			}
			if(ps != null){
				ps.close();	
			}
			if(conn != null){
				conn.close();	
			}
			if(this.conn != null){
				this.conn.close();	
			}
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}
}
